<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use DB;
class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // return "浏览";
        $data = DB::table("news")->get();
        //view("分配模板","分配数据");
        return view('test.index',['data'=>$data]);
    }




    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // return "访问添加页面";
        return view('test.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // return "执行添加页面";
        $time = time();
        // var_dump($time);die;
        $res = DB::table('news')->insert(['title'=>$request->title,'username'=>$request->username,'updatetime'=>$time]);
        if($res>0){
            echo "添加成功";
        }else{
            echo "添加失败";
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "浏览单个页面";
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        // return "加载编辑页面";
        $id = $request->route("test");//获取路由参数
        // var_dump($id);die;
        $data = DB::table("news")->where('id',$id)->get();
        // var_dump($data);die;
        return view('test.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
         // return "更新修改页面";
        // var_dump($request);
        $time=time();
        $res = DB::table('news')->where('id',$request->id)->update(['title'=>$request->title,'username'=>$request->username,'updatetime'=>$time]);
        if($res){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return "删除页面";
    }

    //删除操作
    public function delete(Request $request){
        // echo "delete";
        $res = DB::table('news')->where('id',$request->id)->delete();
        if($res){
            echo "删除成功";
            // return view('test.index');
        }else{
            echo "删除失败";
        }
    }

    public function doUpload(Request $request){
       if($request->hasFile("upfile")){
            //获取上传信息
            $file = $request->file("upfile");
            //确认上传的文件是否成功
            if($file->isValid()){
                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $filename = time().rand(1000,9999).".".$ext;
                $file->move("./upload/",$filename);
                                
                return response($filename); //输出
                exit();
            }
        }
        // echo 111;
     }
      

    public function response(){
        // echo 111;die;
        $content = "hello Laravel";
        $status = 403;
        $value = "text/html";
        // return (new Response($content,$status))->header('Content-Type',$value);
        // return Response()->json(["name"=>"hello"]);
        // return response("aa",200)->header('lamp','186');
        // return response("aa",200)->header('lamp','186')->cookie("afaf","afa");
        // return redirect('/home');
        return redirect('shancun')->with('ccccc','bbbb');

    }

    public function shancun(){
        echo session("ccccc");
    }
}
